package com.yinuo.lai.common.util;

/**
 * Created by Administrator on 2018/5/8.
 */
public class StringUtil {
    /**
     *  获取string 数组对象 的字符串表示形式
     * @param quotationMarks  传入字符串的 包围方式：例如："'" = 'a',"\"" = "a","|" = |a|
     *                       特殊："()" = (a),"{}"={a},"[]"=[a]
     * @param delimit 分隔符 传null 默认逗号
     * @return
     */
    public static String arrayConvertToStr(String quotationMarks,String delimit,String[] arr){
        String quotationMarksStart = null;
        String quotationMarksEnd = null;
        if(null==quotationMarks||"".equals(quotationMarks.trim())){
            quotationMarks = "";
        }else{
            if("()".equals(quotationMarks)){
                quotationMarksStart = "(";
                quotationMarksEnd = ")";
            }else if("{}".equals(quotationMarks)){
                quotationMarksStart = "{";
                quotationMarksEnd = "}";
            }else if("[]".equals(quotationMarks)){
                quotationMarksStart = "[";
                quotationMarksEnd = "]";
            }else{
                quotationMarksStart = quotationMarks;
                quotationMarksEnd = quotationMarks;
            }
        }
        if(null==delimit||"".equals(delimit.trim())){
            delimit = ",";
        }
        StringBuffer sb = new StringBuffer();
        for (String s: arr) {
            sb.append(quotationMarksStart).append(s).append(quotationMarksEnd).append(delimit);
        }
        if(sb.lastIndexOf(",")!=-1)sb.deleteCharAt(sb.lastIndexOf(","));
        return sb.toString();
    }

    /**
     *
     * @param split 传入字符串的拆分规则
     * @param quotationMarks 传入字符串的 包围方式：例如："'" = 'a',"\"" = "a","|" = |a|
     *                       特殊："()" = (a),"{}"={a},"[]"=[a]
     * @param str
     * @return
     */
    public static String[] stringConvertToArr(String split,String quotationMarks,String str){
        String quotationMarksStart = null;
        String quotationMarksEnd = null;
        if(null==quotationMarks||"".equals(quotationMarks.trim())){
            quotationMarksStart = "";
            quotationMarksEnd = "";
        }else{
            if("()".equals(quotationMarks)){
                quotationMarksStart = "(";
                quotationMarksEnd = ")";
            }else if("{}".equals(quotationMarks)){
                quotationMarksStart = "{";
                quotationMarksEnd = "}";
            }else if("[]".equals(quotationMarks)){
                quotationMarksStart = "[";
                quotationMarksEnd = "]";
            }else{
                quotationMarksStart = quotationMarks;
                quotationMarksEnd = quotationMarks;
            }
        }

        str = str == null ? "" : str;
        String[] tempArr = str.split(split);
        String[] resultArr = new String[tempArr.length];
        StringBuilder sb = new StringBuilder(10);
        for(int i=0;i<tempArr.length;i++){
            resultArr[i] = sb.append(quotationMarksStart).append(tempArr[i]).append(quotationMarksEnd).toString();
            sb.delete(0,sb.length());
        }
        return resultArr;
    }

    public static void main(String[] args) {
        //System.out.println(Arrays.toString(stringConvertToArr(",","'","1,2,3,4,5,")));
        String[]a = {"1","2","3"};
        System.out.println(arrayConvertToStr("()",",",a));
    }
}
